<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"
%><%@ taglib prefix="func" uri="http://www.zz.com/tag"
%><%@ tag pageEncoding="UTF-8"
%><%@ attribute name="tableName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="key" required="true" rtexprvalue="true"  description=""
%><%@ attribute name="value" required="true" rtexprvalue="true"  description=""
%><%@ attribute name="groupId" required="false" rtexprvalue="true" description=""
%><%@ attribute name="whereConditon" required="false" rtexprvalue="true" description=""
%><%@ attribute name="keyFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="valueFieldName" required="true" rtexprvalue="true" description=""
%><%@ attribute name="relateKeyFieldName" required="false" rtexprvalue="true" description=""
%><%@ attribute name="relateValueFieldName" required="false" rtexprvalue="true" description=""
%><%@ attribute name="defaultKey" required="false" rtexprvalue="true" description=""
%><%@ attribute name="defaultValue" required="false" rtexprvalue="true" description=""
%><%@ attribute name="isDisplayKey" required="false" rtexprvalue="true" description=""
%><%@ attribute name="onchange" required="false" rtexprvalue="true" description=""
%><%@ attribute name="readonly" required="false" rtexprvalue="true" description=""
%><c:set var="basePath" value="${pageContext.request.contextPath}"/>
<%@ attribute name="css" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="relateCondition" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="maxRows" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="placeholder" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="customData" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="isCache" required="false" rtexprvalue="true" description="" %>
<c:if test="${not empty defaultKey}">
    <c:if test="${'DICT_ITEM' eq tableName}">
        <c:set var="defaultValue" value="${empty defaultValue ? func:dictItem(groupId, defaultKey) : defaultValue}"/>
    </c:if>
    <c:if test="${'DICT_ITEM' ne tableName}">
        <c:set var="defaultValue"
               value="${empty defaultValue ? func:dictName(tableName, defaultKey, key, value) : defaultValue}"/>
    </c:if>
</c:if>
<input type="hidden" value="${defaultKey}" id="${keyFieldName}" name="${keyFieldName}"
       datafield="SearchTable.${keyFieldName}">
<input type="text" value="${defaultValue}" id="${valueFieldName}" name="${valueFieldName}"
       datafield="SearchTable.${valueFieldName}">

<c:set var="autoId" value="${fn:replace(valueFieldName, '.', '_')}"/>
<script>
    var _split = "------===";

    $(document).ready(function () {
        var ${autoId} =
        $("[id='${valueFieldName}']").tautocomplete({
            relateKey: "${keyFieldName}",
            placeholder: '${empty placeholder ? "请输入关键字" : placeholder}',
            width: "400px",
            css: "${css}",
            columns: ['编码', '名称'],
            regex: "^[a-zA-Z0-9\\s]+$",
            <c:choose>
            <c:when test="${empty customData}">
            ajax: {
                url: "${basePath}/cache/getBlurDict",
                type: "POST",
                data: function () {
                    var searchData = ${autoId}.searchdata();
                    var whereConditon = "${whereConditon}";
                    <c:if test="${not empty relateCondition}">
                    whereConditon += ${relateCondition}();
                    </c:if>
                    return {
                        "name_startsWith": searchData,
                        "tableName": '${tableName}',
                        "maxRows": '${empty maxRows ? '11' : maxRows}',
                        "key": '${key}',
                        "value": '${value}',
                        "whereConditon": whereConditon,
                        "groupId": "${groupId}",
                        "isCache": "${isCache}"
                    };
                },
                success: function (jsonData) {
                    var filterData = [];
                    $.each(jsonData, function (i, v) {
                        var obj = new Object();
                        obj.id = v['${value}'] + _split + v['${key}'];

                        obj.KEY = v['${key}'];
                        obj.VALUE = v['${value}'];

                        filterData.push(obj);
                    });
                    return filterData;
                }
            },
            </c:when>
            <c:otherwise>
            ajax: {
                url: "${basePath}/cache/getBlurDict",
                type: "POST",
                data: function () {
                    var searchData = {};
                    return {
                        "name_startsWith": searchData,
                        "tableName": '${tableName}',
                        "maxRows": '${empty maxRows ? '11' : maxRows}',
                        "key": '${key}',
                        "value": '${value}',
                        "groupId": "${groupId}"
                    };
                },
                success: function (jsonData) {
                    return ${customData};
                }
            },
            </c:otherwise>
            </c:choose>
            onchange: function () {
                var datas =
                ${autoId}.
                id().split(_split);

                var value = datas[0] || '';
                var key = datas[1] || '';
                $("[id='${keyFieldName}']").val(key);
                $("[id='${valueFieldName}']").val(value);

                var el = ${autoId};
                var displayTxt = ${'true' == isDisplayKey} ? key : (key + "/" + value);
                displayTxt = displayTxt == '/' ? '' : displayTxt;
                el.ddTextbox.data("text", displayTxt);
                el.ddTextbox.val(displayTxt);

                if (${not empty relateKeyFieldName}) {
                    $("#${relateKeyFieldName}").val(key);
                }

                if (${not empty relateValueFieldName}) {
                    $("#${relateValueFieldName}").val(value);
                }

                if (${not empty onchange}) {
                    ${onchange}(key, value);
                }
                return;
            }
        });

        var displayTxt = ${'true' == isDisplayKey} ? '${defaultValue}' : ('${defaultKey}' + "/" + '${defaultValue}');
        displayTxt = displayTxt == '/' ? '' : displayTxt;

        var displayField = $("#${valueFieldName}").closest("div").find('input[autocomplete="off"]');
        displayField.data("text", displayTxt).val(displayTxt);
        if ("${readonly}" == "readonly") {
            displayField.attr("readonly", "readonly");
            displayField.attr("isNoCrear", "1");
        }

    });

</script>
